package com.example.parkingproject.service.serviceImp;

import com.alibaba.fastjson.JSON;
import com.example.parkingproject.code.ServiceCode;
import com.example.parkingproject.ex.ServiceException;
import com.example.parkingproject.jopo.dto.UserLoginDto;
import com.example.parkingproject.jopo.dto.UserRegDto;
import com.example.parkingproject.jopo.dto.UserUpdateDto;
import com.example.parkingproject.jopo.entity.Password;
import com.example.parkingproject.jopo.entity.User;
import com.example.parkingproject.jopo.vo.*;
import com.example.parkingproject.mapper.UserMapper;
import com.example.parkingproject.service.UserService;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.rmi.server.ServerCloneException;
import java.util.*;

/**
 * usersevice 接口实现类
 */
@Slf4j
@Service
public class UserServiceImp implements UserService {
    @Autowired
    UserMapper userMapper;

    @Autowired
    PasswordEncoder passwordEncoder;


    /**
     * 处理用户登录
     * @param userLoginDto
     */
    @Override
    public UserLoginVo login(UserLoginDto userLoginDto) {
        UserLoginVo result = userMapper.getUserByUsername(userLoginDto.getUsername());

        log.debug("获取到的参数{}",result);
        if (result==null){
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,"用户不存在");
        }

        if (userLoginDto.getUsername().equals(result.getUsername())&&userLoginDto.getPassword().equals(result.getPassword())){
            return result;
        }else {
            throw new ServiceException(ServiceCode.error_bad_request,"密码错误");
        }

    }

    /**
     * 处理用户注册
     * @param userRegDto
     */
    @Override
    public void reg(UserRegDto userRegDto) {
        UserRegVo userRegVo = userMapper.selectUserRegVoByUsername(userRegDto);
        if (userRegVo != null) {
            String message = "用户名已存在，请直接登录";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }

        UserRegVo userStanderdByPhone = userMapper.getUserStanderdByPhone(userRegDto.getPhone());
        if (userStanderdByPhone!=null){
            String message = "手机号已经占用";
            throw new ServiceException(ServiceCode.error_bad_request,message);
        }

        UserRegVo userStanderdByCardId = userMapper.getUserStanderdByCardId(userRegDto.getCardId());

        if (userStanderdByCardId!=null){
            String message = "身份证号已经占用";
            throw new ServiceException(ServiceCode.error_bad_request,message);
        }


        User user = new User();
        BeanUtils.copyProperties(userRegDto, user);
        user.setCreateTime(new Date());
        String password = user.getPassword();
        log.debug("{}",password);
        userMapper.RegUser(user);

        userMapper.addCar(userRegDto);
    }

    @Override
    public getUserVo getUserByCardId(String cardId) {
        return userMapper.getUserByCardId(cardId);
    }

    @Override
    public void update(UserUpdateDto userUpdateDto) {
        getUserVo userByCardId = userMapper.getUserByCardId(userUpdateDto.getCardId());
        if (userByCardId==null){
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,"该用户不存在");
        }
        userMapper.update(userUpdateDto);
    }

    @Override
    public List<spaceOccupancy> getSpaceOccupancy(String carId) {
        List<spaceOccupancy> list = userMapper.getSpaceOccupancy(carId);
        if (list.size()==0){
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,"没有该车辆信息");
        }
        return list;
    }

    @Override
    public List<StopHistoryVo> getHistory(String cardId) {

        List<StopHistoryVo> list = userMapper.getHistory(cardId);
        return list;
    }

    @Override
    public List<StopHistoryVo> search(StopHistoryVo stopHistoryVo) {
        log.debug("传入参数{}",stopHistoryVo);
        List<StopHistoryVo> list = userMapper.search(stopHistoryVo);
        if (list.size()==0){
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,"查询数据不存在");
        }
        return list;
    }

    @Override
    public Integer getUserNumber() {
        Integer count = userMapper.getUserNumber();
        return count;
    }

    @Override
    public void openVip(String cardId) {
        Integer row = userMapper.getVip(cardId);
        log.debug("获取VIP结果{}", row);
        if (row!=0){
            throw new ServiceException(ServiceCode.error_bad_request,"您已经是vip");
        }
        userMapper.openVip(cardId);

    }

    @Override
    public void updatePassword(Password password) {
        log.debug("密码参数{}",password);
        Integer count = userMapper.getPassword(password);
        if (count==null){
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,"旧密码错误");
        }
        userMapper.updatePassword(password);
    }

    @Override
    public void updatePasswordByPhone(Password password, String code) {


        if (code ==null){
            throw new ServiceException(ServiceCode.error_bad_request,"验证码已过期");
        }
        if (!password.getSms().equals(code)){
            throw new ServiceException(ServiceCode.error_bad_request,"验证码错误");
        }else {
            userMapper.updatePasswordByPhone(password);
        }
    }

}
